#include"String8.h"
#include <string>


CString8::CString8()
{
    Data.Add(u8'\0');
}

CString8::CString8(const char8_t *InUtf8String)
{
    size_t Length = std::char_traits<char8_t>::length(InUtf8String);
    Data.Resize(Length + 1);
    std::char_traits<char8_t>::copy(Data.Data(), InUtf8String, Length);
    Data[Length] = u8'\0';
}

CString8::CString8(int64_t InNum)
{
    auto NumStr = std::to_string(InNum);
    size_t Length = NumStr.length();
    Data.Resize(Length + 1);
    std::char_traits<char8_t>::copy(Data.Data(), (char8_t *)NumStr.c_str(), Length);
    Data[Length] = u8'\0';
}

char *CString8::CStr() const
{
    return (char *)Data.Data();
}

char8_t *CString8::CStr8() const
{
    return Data.Data();
}

size_t CString8::CStrLength() const
{
    return Data.Num() - 1;
}

TVector<char8_t> CString8::GetData() const
{
    return Data;
}

CString8 CString8::operator+(const CString8 &Other) const
{
    CString8 NewString;
    NewString.Data.Resize(Data.Num() + Other.Data.Num() - 1);
    std::char_traits<char8_t>::copy(NewString.Data.Data(), Data.Data(), Data.Num() - 1);
    std::char_traits<char8_t>::copy(NewString.Data.Data() + Data.Num() - 1, Other.Data.Data(), Other.Data.Num());
    return NewString;
}

CString8 &CString8::operator+=(const CString8 &Other)
{
    Data.Resize(Data.Num() + Other.Data.Num() - 1);
    std::char_traits<char8_t>::copy(Data.Data() + Data.Num() - 1, Other.Data.Data(), Other.Data.Num());
    return *this;
}

CString8 CString8::operator+(const char8_t *InUtf8String) const
{
    size_t Length = std::char_traits<char8_t>::length(InUtf8String);
    CString8 NewString;
    NewString.Data.Resize(Data.Num() + Length);
    std::char_traits<char8_t>::copy(NewString.Data.Data(), Data.Data(), Data.Num() - 1);
    std::char_traits<char8_t>::copy(NewString.Data.Data() + Data.Num() - 1, InUtf8String, Length);
    NewString.Data[Data.Num() + Length - 1] = u8'\0';
    return NewString;
}

CString8 &CString8::operator+=(const char8_t *InUtf8String)
{
    size_t Length = std::char_traits<char8_t>::length(InUtf8String);
    Data.Resize(Data.Num() + Length);
    std::char_traits<char8_t>::copy(Data.Data() + Data.Num() - 1, InUtf8String, Length);
    Data[Data.Num() + Length - 1] = u8'\0';
    return *this;
}

bool CString8::operator==(const CString8 &Other) const
{
    return Data == Other.Data;
}
